package club.pchome.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import club.pchome.common.constant.SystemConstant;
import club.pchome.common.entity.CheckResult;
import club.pchome.common.util.JwtUtils;
import club.pchome.dto.Result;
import club.pchome.dto.UserInfoDto;
import club.pchome.service.AutoSignService;
import club.pchome.service.UserInfoService;
import club.pchome.util.PageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
public class ManageController {
	private static final Logger logger = LoggerFactory.getLogger(ManageController.class);
	@Autowired
	AutoSignService autoSignService;
	
	@Autowired
	UserInfoService userInfoService;
	
	//获取用户管理
	@ApiOperation(value = "用户列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "holder", value = "归属人"), @ApiImplicitParam(name = "currPage", value = "当前页"), @ApiImplicitParam(name = "pageSize", value = "每页条数,0为全部")})
	@PostMapping("/getUserList")
	public PageUtil<UserInfoDto> userList(HttpServletRequest request, String holder, String page, String limit){
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("holder", holder);
		PageUtil<UserInfoDto> pu = new PageUtil<UserInfoDto>(page, limit);
		return autoSignService.getUserList(pu , map);
	}
	
	/**
	 *  验证登录状态
	 * @param token
	 * @return
	 */
	@PostMapping("/checkResult")
	public Result<String> CheckResult(String token) {
		//验证JWT的签名，返回CheckResult对象
		CheckResult checkResult = JwtUtils.validateJWT(token);
		if (checkResult.isSuccess()) {
			return Result.initSuccessResult("OK");
		} else {
			switch (checkResult.getErrCode()) {
				// 签名验证不通过
				case SystemConstant.JWT_ERRCODE_FAIL:
					logger.info("签名验证不通过");
					
					Result.initFailureResult(SystemConstant.JWT_ERRCODE_FAIL, "签名验证不通过");
					break;
					// 签名过期，返回过期提示码
				case SystemConstant.JWT_ERRCODE_EXPIRE:
					logger.info("签名过期");
					Result.initFailureResult(SystemConstant.JWT_ERRCODE_EXPIRE, "签名过期!!");
					break;
				default:
					break;
			}
			return Result.initFailureResult(SystemConstant.JWT_ERRCODE_NULL, "签名验证不通过, 请重新获取!!!");
		}
	}
	
	
	/**
	 * 当日短信数
	 */
	@PostMapping("/toDaySendSMSCount")
	public Result<Integer> toDaySendSMSCount(String token) {
		int count = autoSignService.toDaySendSMSCount();
		return Result.initSuccessResult(count);
	}
}
